//
//  WJAddPDFPopupView.swift
//  PDFSign
//
//  Created by 李永彬 on 2025/9/20.
//

import UIKit

protocol WJAddPDFPopupViewDelegate: AnyObject {
    func didClickCameraItem(_ popupView: WJAddPDFPopupView)
    func didClickGalleryItem(_ popupView: WJAddPDFPopupView)
    func didClickFilesItem(_ popupView: WJAddPDFPopupView)
}

class WJAddPDFPopupView: WJBasePopupView {
    
    weak var delegate: WJAddPDFPopupViewDelegate? = nil
    
    lazy var fromCameraItem: WJAddPdfItemView = {
        let itemView = WJAddPdfItemView()
        itemView.updateView(imageName: "from_camera", name: "From Camera")
        itemView.addTarget(self, action: #selector(clckCameraAction), for: .touchUpInside)
        return itemView
    }()
    
    lazy var fromGalleryItem: WJAddPdfItemView = {
        let itemView = WJAddPdfItemView()
        itemView.updateView(imageName: "from_gallery", name: "From Gallery")
        itemView.addTarget(self, action: #selector(clckGalleryAction), for: .touchUpInside)
        return itemView
    }()
    
    lazy var fromFilesItem: WJAddPdfItemView = {
        let itemView = WJAddPdfItemView()
        itemView.updateView(imageName: "from_files", name: "From Files")
        itemView.addTarget(self, action: #selector(clckFilesAction), for: .touchUpInside)
        return itemView
    }()
    
    lazy var vStackView: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.distribution = .fillEqually
        stackView.alignment = .fill
        stackView.spacing = 16.0
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.addArrangedSubview(fromCameraItem)
        stackView.addArrangedSubview(fromGalleryItem)
        stackView.addArrangedSubview(fromFilesItem)
        return stackView
    }()
    
    override func setupContent() {
        super.setupContent()
        setupContentView()
        setupConstraints()
    }
    
    private func setupContentView() {
        contentView.addSubview(vStackView)
    }
    
    private func setupConstraints() {

        vStackView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(16)
            make.height.equalTo(224)
        }
    }
    
    override func setupContentViewPosition(for style: WJPopupAnimationStyle) {
        contentView.snp.makeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
            make.height.equalTo(290)
        }
        
        contentView.setNeedsLayout()
        contentView.layoutIfNeeded()
        
        switch style {
        case .fromBottom:
            contentView.frame.origin.y = bounds.height
        case .fromCenter:
            contentView.center.y = bounds.height / 2
            contentView.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
            contentView.alpha = 0
        case .fromTop:
            contentView.frame.origin.y = -contentView.frame.height
        }
    }
    
    override func setupContentViewFinalPosition() {
        switch animationStyle {
        case .fromBottom:
            contentView.frame.origin.y = bounds.height - contentView.frame.height
        case .fromCenter:
            contentView.center.y = bounds.height / 2
            contentView.transform = .identity
            contentView.alpha = 1
        case .fromTop:
            contentView.frame.origin.y = 20
        }
    }
    
    @objc func clckCameraAction() {
        dismiss { [weak self] in
            guard let self = self else { return }
            self.delegate?.didClickCameraItem(self)
        }
    }
    
    @objc func clckGalleryAction() {
        dismiss { [weak self] in
            guard let self = self else { return }
            self.delegate?.didClickGalleryItem(self)
        }
    }
    
    @objc func clckFilesAction() {
        dismiss { [weak self] in
            guard let self = self else { return }
            self.delegate?.didClickFilesItem(self)
        }
    }
}

class WJAddPdfItemView: UIControl {
    
    lazy var iconImgView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFit
        return imgView
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .left
        label.font = UIFont.montserratRegular(16)
        label.textColor = UIColor(hex: "#121212")
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        layer.masksToBounds = true
        layer.cornerRadius = 12
        layer.borderWidth = 0.5
        layer.borderColor = UIColor(hex: "#121212", alpha: 0.05).cgColor
        commonInitSubviews()
    }
    
    func commonInitSubviews() {
        addSubview(iconImgView)
        addSubview(titleLabel)
        
        iconImgView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalToSuperview().offset(25)
            make.width.height.equalTo(48)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalTo(iconImgView)
            make.leading.equalTo(iconImgView.snp.trailing).offset(10)
            make.height.equalTo(24)
            make.trailing.equalToSuperview().offset(-10)
        }
    }
    
    func updateView(imageName: String, name: String) {
        iconImgView.image = UIImage(named: imageName)
        titleLabel.text = name
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
